<template>
  <div class="user-wrapper" :class="theme">
    <a-dropdown>
      <span class="action action-full ant-dropdown-link user-dropdown-menu">
        <a-avatar class="avatar" size="small" :src="avatar_fileUrl"/>
        <span v-if="isDesktop()">欢迎您，{{ realname }}</span>
      </span>

      <a-menu slot="overlay" class="user-dropdown-menu-wrapper">
        <a-menu-item @click="updatePassword">
          <a-icon type="edit"/>
          <span>密码修改</span>
        </a-menu-item>
        <a-menu-item @click="personSetting">
          <a-icon type="setting"/>
          <span>个性设置</span>
        </a-menu-item>
        <a-menu-item @click="refreshUserPermission">
          <a-icon type="redo"/>
          <span>刷新权限</span>
        </a-menu-item>
      </a-menu>
    </a-dropdown>
    <span class="action">
      <a class="logout_title" href="javascript:;" @click="handleLogout">
        <a-icon type="logout"/>
        <span v-if="isDesktop()">&nbsp;退出登录</span>
      </a>
    </span>
    <user-password ref="userPassword"></user-password>
    <setting-drawer ref="settingDrawer"></setting-drawer>
  </div>
</template>

<script>
  import UserPassword from './UserPassword';
  import { mixinDevice, } from '../../mixin/store-mixin';
  import {postAction} from '../../api/manage';
  import { userInfo, common } from '../../api/url'
  import SettingDrawer from "../setting/SettingDrawer";
  import { loginRouterPath } from '../../config/router-constant-table'

  export default {
    name: "UserMenu",
    mixins: [mixinDevice],
    components: {
      SettingDrawer,
      UserPassword,
    },
    computed: {
      avatar_fileUrl() {
        return this.$store.getters.avatar_fileUrl;
      },
      realname() {
        return this.$store.getters.realname;
      },
      username() {
        return this.$store.getters.username;
      },
      userType() {
        return this.$store.getters.userType;
      }
    },
    data(){
      return {
         logoutUrl:common.logout,
      }
    },
    props: {
      theme: {
        type: String,
        required: false,
        default: 'dark'
      }
    },
    mounted(){
      this.logoutUrl = common.logout;

    },
    methods: {

      handleLogout() {
        const that = this

        this.$confirm({
          title: '提示',
          content: '真的要注销登录吗 ?',
          onOk() {
            return postAction(that.logoutUrl)
              .then(()=>{
                that.$store.commit("CLEAR_USER")
                that.$router.push({path:loginRouterPath})
              }).catch(err => {
              that.$message.error({
                title: '错误',
                description: err.message
              })
            })
          },
          onCancel() {
          },
        });
      },
      updatePassword(){
        let username = this.username
        this.$refs.userPassword.show(username)
      },
      personSetting(){
        this.$refs.settingDrawer.showDrawer();
      },
      refreshUserPermission(){
        postAction(userInfo.refreshUserPermission)
          .then(res=>{
            if(res.success){
              this.$message.success(res.success);
              window.location.reload();
            }
          })
      }
    }
  }
</script>

<style scoped>
  .logout_title {
    color: inherit;
    text-decoration: none;
  }
</style>